package com.android.permission;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.util.Log;

import com.android.permission.rom.RomType;
import com.android.permission.rom.RomUtils;

/**
 * @ClassName: OpenAutoStartUtil
 * @Desciption: 引导打开自启动权限
 * @author: yichaohua
 * @date: 2018-05-09
 */
public class OpenAutoStartUtil {

    private static final String TAG = "OpenAutoStartUtil";

    public static void jumpStartInterface(Context context) {
        RomType romType = RomUtils.getRomType(context);
        Log.i(TAG,"romType=" + romType.toString());

        if(romType == RomType.SAMSUNG_ROM){
            openSamsungSmat(context);
            return;
        }else if(romType == RomType.SMARTISAN_ROM){
            openSmartPermission(context);
            return;
        }else if(romType == RomType.FLYME_ROM){
            openFlymeBackManager(context);
            return;
        }else if(romType == RomType.COLOROS_ROM){
            openColorOsManager(context);
            return;
        }else if(romType == RomType.FUNTOUCHOS_ROM){
            openVivoManager(context);
            return;
        }

        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        ComponentName componentName = null;

        if(romType == RomType.MIUI_ROM){
            componentName = new ComponentName("com.miui.securitycenter",
                    "com.miui.permcenter.autostart.AutoStartManagementActivity");
        }else if(romType == RomType.EMUI_ROM){
            componentName = new ComponentName("com.huawei.systemmanager",
                    "com.huawei.systemmanager.startupmgr.ui.StartupNormalAppListActivity");
        }else if(romType == RomType.FLYME_ROM){
            // 通过测试，发现魅族是真恶心，也是够了，之前版本还能查看到关于设置自启动这一界面，
            // 系统更新之后，完全找不到了，心里默默Fuck！
            // 针对魅族，我们只能通过魅族内置手机管家去设置自启动，
            // 所以我在这里直接跳转到魅族内置手机管家界面，具体结果请看图
            componentName = ComponentName.unflattenFromString("com.meizu.safe" +
                    "/.permission.PermissionMainActivity");
        }else if(romType == RomType.COLOROS_ROM){
            componentName = ComponentName.unflattenFromString("com.oppo.safe" +
                    "/.permission.startup.StartupAppListActivity");
            Intent intentOppo = new Intent();
            intentOppo.setClassName("com.oppo.safe/.permission.startup",
                    "StartupAppListActivity");
            if (context.getPackageManager().resolveActivity(intentOppo, 0) == null) {
                componentName = ComponentName.unflattenFromString("com.coloros.safecenter" +
                        "/.startupapp.StartupAppListActivity");
            }
        }else if(romType == RomType.FUNTOUCHOS_ROM){
            componentName = ComponentName.unflattenFromString("com.iqoo.secure" +
                    "/.MainActivity");
        }else if (getMobileType().equals("Letv")) { // 乐视2测试通过
            intent.setAction("com.letv.android.permissionautoboot");
        }else if (getMobileType().equals("samsung")) { // 三星Note5测试通过
            componentName = new ComponentName("com.samsung.android.sm_cn",
                    "com.samsung.android.sm.ui.ram.AutoRunActivity");
        }else if(RomUtils.checkIs360Rom()){
            componentName = new ComponentName("com.yulong.android.coolsafe",
                    ".ui.activity.autorun.AutoRunListActivity");
        } else {
            // 以上只是市面上主流机型，由于公司你懂的，所以很不容易才凑齐以上设备
            // 针对于其他设备，我们只能调整当前系统app查看详情界面
            // 在此根据用户手机当前版本跳转系统设置界面
            if (Build.VERSION.SDK_INT >= 9) {
                intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                intent.setData(Uri.fromParts("package", context.getPackageName(), null));
            } else if (Build.VERSION.SDK_INT <= 8) {
                intent.setAction(Intent.ACTION_VIEW);
                intent.setClassName("com.android.settings",
                        "com.android.settings.InstalledAppDetails");
                intent.putExtra("com.android.settings.ApplicationPkgName",
                        context.getPackageName());
            }
        }

        intent.setComponent(componentName);

        try {
            context.startActivity(intent);
        }catch (Exception e) {//抛出异常就直接打开设置页面
            e.printStackTrace();
            intent = new Intent(Settings.ACTION_SETTINGS);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            context.startActivity(intent);
        }

    }

    private static String getMobileType() {
        return Build.MANUFACTURER;
    }

    public static void openSamsungSmat(Context context) {
        try {
            Intent intent = new Intent();
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
                intent.setComponent(new ComponentName("com.samsung.android.sm_cn",
                        "com.samsung.android.sm.ui.cstyleboard.SmartManagerDashBoardActivity"));
            }else {
                intent.setComponent(new ComponentName("com.samsung.android.sm",
                        "com.samsung.android.sm.app.dashboard.SmartManagerDashBoardActivity"));
            }

            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void openSmartPermission(Context context){
        try {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName("com.smartisanos.security",
                    "com.smartisanos.security.PermissionsActivity"));
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void openFlymeBackManager(Context context){
        try {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName("com.meizu.safe",
                    "com.meizu.safe.permission.SmartBGActivity"));
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void openColorOsManager(Context context){
        ComponentName componentName = ComponentName.unflattenFromString("com.oppo.safe" +
                "/.permission.startup.StartupAppListActivity");
        Intent intentOppo = new Intent();
        intentOppo.setClassName("com.oppo.safe/.permission.startup",
                "StartupAppListActivity");
        if (context.getPackageManager().resolveActivity(intentOppo, 0) == null) {
            componentName = ComponentName.unflattenFromString("com.coloros.safecenter" +
                    "/.startupapp.StartupAppListActivity");
        }

        try {
            Intent intent = new Intent();
            intent.setComponent(componentName);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } catch (Exception ex) {
            ex.printStackTrace();

            try {
                Intent intent = new Intent();
                intent.setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.privacypermissionsentry.PermissionTopActivity"));
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }catch (Exception e) {
                ex.printStackTrace();
                Intent intent = new Intent();
                intent = new Intent(Settings.ACTION_SETTINGS);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                context.startActivity(intent);
            }

        }
    }

    public static void openVivoManager(Context context){
        try {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName("com.iqoo.secure", "com.iqoo.secure.MainActivity"));
            context.startActivity(intent);
        }catch (Exception e){
            e.printStackTrace();

            try {
                Intent intent = new Intent();
                intent.setComponent(new ComponentName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.PurviewTabActivity"));
                context.startActivity(intent);
            } catch (Exception ex) {
                ex.printStackTrace();

                Intent intent = new Intent(Settings.ACTION_SETTINGS);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                context.startActivity(intent);
            }
        }

    }
}
